package com.ruoyi.elasticsearch.core;
import com.ruoyi.elasticsearch.emq.EmqClient;
import com.ruoyi.elasticsearch.service.QuotaService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Slf4j
public class Monitor {

    @Autowired
    private EmqClient emqClient;
    @Autowired
    private QuotaService quotaService;
    @PostConstruct
    public void init(){
        System.out.println("初始化方法，订阅主题");
        emqClient.connect();

        quotaService.getAllSubject().forEach(s -> {
            //共享订阅模式
            try {
                emqClient.subscribe("$queue/"+s);
            } catch (MqttException e) {
                e.printStackTrace();
            }
        });

    }
}
